/**
 * 
 */
package com.wubo.bm.user.action;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import com.wubo.bm.user.entity.UserInfo;
import com.wubo.bm.user.service.IUserInfoService;
import com.wubo.common.web.BaseAction;
import com.wubo.sec.SecurityManager;
import com.wubo.sec.core.UserData;
import com.wubo.sec.model.User;
import com.wubo.sec.service.IUserService;

/**
 * @author WuBo
 * @CreateDate 2012-3-2 下午02:43:04
 */
@Controller
public class UserCenterMgtAction extends BaseAction{
	@Resource
	private IUserInfoService userInfoService;
	@Resource
	private IUserService userService;
	
	private String username;
	private boolean allowPut = false;
	private UserInfo userInfo;
	private User user;
	private String age;
	
	public String enter() throws Exception {
		UserData curuser =  SecurityManager.getCurrentUser();
		boolean self = false;
		if(! StringUtils.hasText(username)){
			if(curuser == null){
				getHttpServletResponse().sendError(403);
				return null;
			}
			username = curuser.getUsername();
			self = true;
		}else{
			if(curuser != null && curuser.getUsername().equals(username)){
				self = true;
			}
			if(SecurityManager.isAdmin()){
				allowPut = true;
			}
		}
		if(self)allowPut = true;
		user = userService.getByUsername(username);
		userInfo = userInfoService.xget("username", username);
		return SUCCESS;
	}
	
	public String saveBasicInfo() throws Exception {
		userInfo.setUser(user);
		if(StringUtils.hasText(age)){
			userInfo.setAge(Integer.parseInt(age));
		}
		userInfoService.saveOrUpdateBasicInfo(userInfo, user);
		return enter();
	}
	
	public String saveMoreInfo() throws Exception {
		userInfo.setUser(user);
		userInfoService.saveOrUpdateMoreInfo(userInfo, user);
		return enter();
	}
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public boolean isAllowPut() {
		return allowPut;
	}
	public void setAllowPut(boolean allowPut) {
		this.allowPut = allowPut;
	}

	public IUserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(IUserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}
	
}
